//
//  chattingViewController.m
//  ttsdktestapp
//
//  Created by wangna on 14-12-24.
//  Copyright (c) 2014年 wangna. All rights reserved.
//

#import "chattingViewController.h"
#import "MessageFrame.h"
#import "MessageCell.h"
#import "Message.h"
#import "TTChatView.h"
#import "ChatManager.h"
//#import "ttsdkAppDelegate.h"
//#import "createGroupViewController.h"

@interface ChattingViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,TTChatViewDelegate>
{
    NSMutableArray *_allMessagesFrame;
}
@end

@implementation ChattingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithNibNameAndObj:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil group:(SDKGroup *)group
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _sdk_group = group;
    }
    return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _allMessagesFrame.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[MessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    // 设置数据
    cell.messageFrame = _allMessagesFrame[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [_allMessagesFrame[indexPath.row] cellHeight];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"click send");
    return YES;
}

- (void)onChat:(NSString *)content
{
    [[ChatManager sharedInstance] sendMsg:@{@"content":content,@"type":@(TTNetMessageTypeCustomContent)} toid:[self.touserid integerValue] callback:^(NSDictionary *resp) {
        int code = [[resp objectForKey:KEY_RESULT_CODE] intValue];
        if (code == 0) {
            
        }
    }];
}

-(void)addMessageData:(NSArray *)array
{
    
    
    if (!_allMessagesFrame) {
        _allMessagesFrame = [NSMutableArray array];
    }
    
    
    
    NSString *previousTime = nil;
    for (NSDictionary *dict in array) {
        
        MessageFrame *messageFrame = [[MessageFrame alloc] init];
        Message *message = [[Message alloc] init];
        message.dict = dict;
        
        messageFrame.showTime = ![previousTime isEqualToString:message.time];
        
        messageFrame.message = message;
        
        previousTime = message.time;
        
        [_allMessagesFrame addObject:messageFrame];
    }
}

- (void)viewDidLoad
{
    _allMessagesFrame = [NSMutableArray array];
    [contentTable setDelegate:self];
    [contentTable setDataSource:self];
    [contentTable setTableHeaderView:headerView];
    //NSArray *messageData = @[@{@"content":@"你妹夫啊，你才2B！说，什么事？",@"icon":@"icon02.png",@"time":@"昨天 20:07",@"type":@1},@{@"content":@"你妹夫啊，你才2B！说，什么事？",@"icon":@"icon02.png",@"time":@"昨天 20:07",@"type":@1},@{@"content":@"你妹夫啊，你才2B！说，什么事？",@"icon":@"icon01.png",@"time":@"昨天 20:07",@"type":@0}];
    //[self addMessageData:messageData];
    _groupidLabel.text = _touserid;
    [chatView showstat:ttchatviewstatnormal];
    [chatView setChatdelegate:self];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    _groupidLabel.text = _touserid;
//    [chatView showstat:ttchatviewstatnormal];
//    NSString * strGroupId = [NSString stringWithFormat:@"%llu", [_sdk_group getId]];
//    _groupidLabel.text = strGroupId;
}

- (void) dealloc
{
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)exitGroup:(id)sender
{
    //退出群
//    ttsdkAppDelegate *delegate = (ttsdkAppDelegate *)[UIApplication sharedApplication].delegate;
//    [delegate.app.sdk_connect exitGroup:[_sdk_group getId]];
}

- (void) onExitGroup:(int32_t)result groupid:(uint64_t)groupid
{
    if (result == 0 && groupid == [_sdk_group getId]) {
        //退群成功
        //显示创建群的界面
//        CreateGroupViewController * createGroupViewController = [[CreateGroupViewController alloc] initWithNibName:@"createGroupViewController" bundle:nil];
//        
//        ttsdkAppDelegate *delegate = (ttsdkAppDelegate *)[UIApplication sharedApplication].delegate;
//        //设置委托
//        [delegate.app.sdk_connect setDelegate:createGroupViewController];
//        //显示界面
//        delegate.window.rootViewController = createGroupViewController;
        
    }
    else
    {
        UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"退群" message:@"退群失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
}

- (void) onJoinGroup:(int32_t)result group:(SDKGroup *)group
{
    
}



@end
